package com.ifast.integral.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ifast.common.utils.Const;
import com.ifast.common.utils.Const.integralExplain;
import com.ifast.integral.dao.IntegralExplainDao;
import com.ifast.integral.domain.IntegralExplain;
import com.ifast.integral.service.IntegralExplainService;
import com.luculent.util.extension.api.Result;
import com.xiaoleilu.hutool.util.CollectionUtil;

import cn.hutool.core.bean.BeanUtil;


/**
 * 
 * @author ShiQiang
 * @date 2019年4月9日09:56:53
 */
@Transactional
@Service("integralExplainService")
public class IntegralExplainServiceImpl extends ServiceImpl<IntegralExplainDao, IntegralExplain> implements IntegralExplainService{

	@Override
	public com.ifast.common.utils.Result<?> save(IntegralExplain explain) {
		return StringUtils.isNotBlank(explain.getId())?(this.updateById(explain)?com.ifast.common.utils.Result.ok():com.ifast.common.utils.Result.fail()):(this.insert(explain)?com.ifast.common.utils.Result.ok():com.ifast.common.utils.Result.fail());
	}

	@Override
	public Result<?> rule(integralExplain expLainTye, String dwCode){
		if(StringUtils.isBlank(dwCode) || dwCode.length() < Const.dwCodeSubLength){ 
			return Result.fail_msg("只有企业和其下级人员能够查看");
	    }  		
		List<IntegralExplain> list = this.selectList(new EntityWrapper<IntegralExplain>()
				.like("DW_CODE", dwCode.substring(0, Const.dwCodeSubLength), SqlLike.RIGHT).eq("CLASSIFY", expLainTye.getId()).orderBy("SORT", false));
		if(CollectionUtil.isEmpty(list)){
			return Result.fail_msg("暂无数据");
		}
		return Result.ok(BeanUtil.beanToMap(list.get(0)));
	}
}